import { pinyin } from './../node_modules/pinyin-pro/dist/index';

/**
 * 主接口
 * @param {Object} text
 * @param {Object} options
 */
const pinyinUtils = function (text, options) {
	return pinyin(text, options);
}

const pinyinMultiple = function (text, options) {
	let rtn = {
		success:false,
		code:'',
		message:"",
		data: null
	}
	if(text.length !== 1) {
		rtn.code = "1001"
		rtn.message = "参数错误"
		console.error("多音字识别，只可识别一个多音字");
		return rtn;
	}
	// 正则校验
	const han = /^[\u4e00-\u9fa5]+$/;
	if(!han.test(text)){
		rtn.code = "1002"
		rtn.message = "参数错误"
		console.error("多音字识别，请传入汉字");
		return rtn;
	}
	// 参数配置
	let _options = {
		multiple: true,
		// 返回类型
		type: 'array',
		// 音调输出形式
		toneType: 'num',
		...options
	}
	// console.log('_options',_options);
	const res = pinyin(text, _options);
	rtn.success = true;
	rtn.data = res;
	return rtn;
}

/**
 * 多音字识别
 * @param {Object} text	html文本内容
 */
const pinyinIdentify = function(text){
	let rtn = [];
	if(text == null || text === '' || text === undefined) {
		return rtn;
	}
	// 参数配置
	let _options = {
		multiple: true,
		// 返回类型
		type: 'array',
		// 音调输出形式
		toneType: 'num',
	}
	// console.log('_options',_options);
	
	// 截取<p>内容
	text = text.replaceAll('</p>','');
	text = text.replaceAll('<br>','');
	text = text.replace('<p>','');
	const textArr = text.split('<p>');
	// 正则校验
	const han = /^[\u4e00-\u9fa5]+$/;
	// 正则校验
	const duyinSingleReg = /\[读音:\S{1,8}\]/;
	const tdSingleReg = /\[停顿:\S{1,8}\]/;
	for (let i = 0; i < textArr.length; i++) {
		let view = {
			name:"view",
			attrs:{},
			childrens:[]
		}
		let textStr = "";
		const itemText = textArr[i];
		for (let i = 0; i < itemText.length; i++) {
			let textVal = itemText[i]
			let dataText = textVal
			// 已标注读已标识
			let mark = false
			// 判断是否是[
			if(dataText === "["){
				// 获取剩余字段
				const residueText = itemText.substring(i,itemText.length)
				// 正则匹配
				let resp =  residueText.match(duyinSingleReg)
				// 判断是否批次成功且第一个就是标注读音的多音字
				if (resp !== null && resp.index === 0) {
					// 获取匹配结果
					const regRespText = resp[0]
					// 获取标注后的多音字
					textVal = regRespText
					// 获取多音字
					dataText = regRespText.slice(-2,-1)
					// 跳过剩余字段
					i += (regRespText.length - 1)
					// 设置已标注读已标识
					mark = true
				}
				else {
					resp =  residueText.match(tdSingleReg)
					if(resp !== null && resp.index === 0){
						// 获取匹配结果
						const regRespText = resp[0]
						// 获取停顿
						textVal = regRespText
						// 跳过剩余字段
						i += (regRespText.length - 1)
						textStr += textVal
						continue;
					}
				}
			}
			if(dataText !== "" && han.test(dataText)){
				const res = pinyin(dataText, _options);
				if (res.length > 1) {
					const preText = {
						name:"text",
						attrs:{},
						text:textStr
					}
					textStr = "";
					const dyzText = {
						name:"text",
						attrs:{
							style: mark ? "color:#009688;" : "color:red;",
							onclick: "changeDuyinBtn"
						},
						text:textVal,
						data:{
							value:dataText,
							multiple:res
						}
					}
					view.childrens.push(preText);
					view.childrens.push(dyzText);
				}
				else {
					textStr += dataText
				}
			}
			else {
				textStr += dataText
			}
		}
		if(textStr !== ""){
			const preText = {
				name:"text",
				attrs:{},
				text:textStr
			}
			view.childrens.push(preText);
		}
		rtn.push(view)
	}
	
	return rtn;
}

/**
 * 多音字配置转内容
 * @param {Object} identifyArr
 */
const identifyToContent = function(identifyArr){
	if(identifyArr === undefined || !(identifyArr instanceof Array)){
		return undefined
	}
	let html = "";
	let respText = "";
	for (let data of identifyArr) {
		const {name,childrens,attrs} = data
		if (data.name === "view") {
			html += "<p>"
			if(data.childrens){
				for (let children of data.childrens) {
					const {attrs,name,text} = children
					if(name === "text"){
						html += text
						respText += text
					}
				}
			}
			
			html += "</p>"
			respText += "\n"
		}
		else if (data.name === "text") {
			html += data.text
			respText += data.text
			respText += "\n"
		}
	}
	return {html,text:respText}
}

module.exports = {
	pinyinUtils,
	pinyinMultiple,
	/**
	 * 多音字识别
	 * @param {Object} text	html文本内容
	 */
	pinyinIdentify,
	identifyToContent
}
